package com.zhny.aiot.three.ball.valve.plugin;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDevice;
import com.zhny.aiot.plugin.core.utils.ConfigUtils;
import com.zhny.aiot.plugin.model.CommandEntry;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.FactoryType;
import com.zhny.aiot.plugin.model.StoreEntity;
import com.zhny.aiot.plugin.zhnygateway.GatewayServer;
import com.zhny.aiot.plugin.zhnygateway.device.ZhnyDeviceFactory;
import com.zhny.aiot.three.ball.valve.plugin.model.BallThreeValveStoreEntry;
import com.zhny.aiot.three.ball.valve.plugin.model.InputParam;
import org.pf4j.Extension;
import org.pf4j.Plugin;
import org.pf4j.PluginWrapper;

import java.util.List;

public class BallThreeValvePlugin extends Plugin {
    public BallThreeValvePlugin(PluginWrapper wrapper) {
        super(wrapper);
    }

    @Extension
    public static class ThreeValveFactory extends ZhnyDeviceFactory<BallThreeValveStoreEntry> {

        @Override
        public List<CommandEntry> supportCommandClasses() {
            String paramJson;
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
            try {
                paramJson = mapper.writeValueAsString(ConfigUtils.getConfigFields(InputParam.class));
            } catch (Exception e) {
                return null;
            }
            List<CommandEntry> commands = super.supportCommandClasses ();
            commands.add(CommandEntry.valueOf("aValue", "A阀门","有参数",paramJson));
            commands.add(CommandEntry.valueOf("bValue", "B阀门","有参数", paramJson));
            commands.add(CommandEntry.valueOf("upDownQuery", "压力上下限查询","无参数", null));
            commands.add(CommandEntry.valueOf("query", "状态查询","无参数", null));
            commands.add(CommandEntry.valueOf("queryPressure", "查询压力","无参数" ,null));
            commands.add(CommandEntry.valueOf("upValue", "压力上限设置","有参数" ,paramJson));
            commands.add(CommandEntry.valueOf("downValue", "压力下限设置","有参数" ,paramJson));
            commands.add(CommandEntry.valueOf("queryVoltage", "查询阀门电压", "无参数",null));
            return commands;
        }

        public IDevice createDevice(ICenterFacilities iCenterFacilities, DeviceEntity entity) {
            return new BallThreeValveDevice (GatewayServer.getInstance ().getEngine () ,this,iCenterFacilities,entity);
        }

        public Class<? extends StoreEntity> storeClass() {
            return BallThreeValveStoreEntry.class;
        }

        public String getId() {
            return "zhny-ball-three-valve-plugin";
        }

        public String getName() {
            return "智慧球型三通阀门插件";
        }

        public FactoryType getType() {
            return FactoryType.ValveController;
        }
    }
}
